package org.summer.lawyer.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.summer.common.core.utils.MapstructUtils;
import cn.hutool.core.util.StrUtil;
import org.summer.common.mybatis.core.page.TableDataInfo;
import org.summer.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.summer.lawyer.domain.vo.maillog.MailLogVo;
import org.summer.lawyer.domain.bo.maillog.MailLogBo;
import org.summer.lawyer.domain.MailLog;
import org.summer.lawyer.mapper.MailLogMapper;
import org.summer.lawyer.service.IMailLogService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 邮件发送记录Service业务层处理
 *
 * @author summer
 * @date 2024-06-29
 */
@RequiredArgsConstructor
@Service
public class MailLogServiceImpl extends ServiceImpl<MailLogMapper, MailLog>  implements IMailLogService {


    /**
     * 查询邮件发送记录
     *
     * @param id 主键
     * @return 邮件发送记录
     */
    @Override
    public MailLogVo selectMailLogVoById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询邮件发送记录列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 邮件发送记录分页列表
     */
    @Override
    public TableDataInfo<MailLogVo> selectPageList(MailLogBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<MailLog> lqw = buildQueryWrapper(bo);
        Page<MailLogVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的邮件发送记录列表
     *
     * @param bo 查询条件
     * @return 邮件发送记录列表
     */
    @Override
    public List<MailLogVo> selectMailLogVoList(MailLogBo bo) {
        LambdaQueryWrapper<MailLog> lqw = Wrappers.lambdaQuery();
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<MailLog> buildQueryWrapper(MailLogBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<MailLog> lqw = Wrappers.lambdaQuery();
        lqw.eq(StrUtil.isNotBlank(bo.getRecipientEmail()), MailLog::getRecipientEmail, bo.getRecipientEmail());
        lqw.eq(StrUtil.isNotBlank(bo.getSubject()), MailLog::getSubject, bo.getSubject());
        lqw.eq(StrUtil.isNotBlank(bo.getBody()), MailLog::getBody, bo.getBody());
        lqw.eq(StrUtil.isNotBlank(bo.getStatus()), MailLog::getStatus, bo.getStatus());
        lqw.eq(bo.getSendTime() != null, MailLog::getSendTime, bo.getSendTime());
        lqw.eq(StrUtil.isNotBlank(bo.getUrl()), MailLog::getUrl, bo.getUrl());
        lqw.eq(StrUtil.isNotBlank(bo.getAttachmentId()), MailLog::getAttachmentId, bo.getAttachmentId());
        lqw.eq(bo.getRetryCount() != null, MailLog::getRetryCount, bo.getRetryCount());
        lqw.eq(StrUtil.isNotBlank(bo.getErrorMessage()), MailLog::getErrorMessage, bo.getErrorMessage());
        return lqw;
    }

    /**
     * 新增邮件发送记录
     *
     * @param bo 邮件发送记录
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(MailLogBo bo) {
        MailLog add = MapstructUtils.convert(bo, MailLog.class);
        validEntityBeforeSave(add);
        add.setSendTime(DateUtil.date());
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改邮件发送记录
     *
     * @param bo 邮件发送记录
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(MailLogBo bo) {
        MailLog update = MapstructUtils.convert(bo, MailLog.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(MailLog entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除邮件发送记录信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
